package com.wl156.activities.find;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.wl156.MyApp;
import com.wl156.R;
import com.wl156.activities.Xinyongxingji;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.utils.Utils;

public class LogisticCompanyInfo extends Activity implements OnClickListener {

	private Button mCollect;
	private TextView mPlace;
	private TextView mName;
	private TextView mContact;
	private TextView mTel;
	private ImageButton mTelDial;
	private TextView mMobile;
	private ImageButton mMobileDial;
	private TextView mCreditScore;
	private TextView mIntro;
	private TextView mBusiness;
	private TextView mFinishOrderCount;
	private TextView mXinyongScore;
	private Button mXinyongDetail;
	private Button mAllSpecialLine;

	private long mLogisticCompanyId;
	private String userID;

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_logistic_company_info);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		mLogisticCompanyId = getIntent().getLongExtra("id", 0);

		mCollect = (Button) findViewById(R.id.collect);
		mCollect.setOnClickListener(this);
		mPlace = (TextView) findViewById(R.id.current_place);
		mName = (TextView) findViewById(R.id.name);
		mContact = (TextView) findViewById(R.id.contact);
		mTel = (TextView) findViewById(R.id.tel);
		mTelDial = (ImageButton) findViewById(R.id.tel_dial);
		mMobile = (TextView) findViewById(R.id.mobile);
		mMobileDial = (ImageButton) findViewById(R.id.mobile_dial);
		mIntro = (TextView) findViewById(R.id.intro);
		mBusiness = (TextView) findViewById(R.id.business);
		mFinishOrderCount = (TextView) findViewById(R.id.finish_order_count);
		mCreditScore = (TextView) findViewById(R.id.credit_score);
		mXinyongScore = (TextView) findViewById(R.id.xinyongscore);
		mXinyongDetail = (Button) findViewById(R.id.xinyongdetail);
		mXinyongDetail.setOnClickListener(this);
		mAllSpecialLine = (Button) findViewById(R.id.allSpecialLine);
		mAllSpecialLine.setOnClickListener(this);

		new AsyncTask<Void, Void, Object>() {
			ProgressDialog mDialog;

			@Override
			protected void onPostExecute(Object result) {
				if (result instanceof SoapObject) {
					SoapObject so = (SoapObject) result;
					String id = Utils.propertyToShow(so.getProperty(0));
					String place = Utils.propertyToShow(so.getProperty(1));
					String name = Utils.propertyToShow(so.getProperty(2));
					String contact = Utils.propertyToShow(so.getProperty(3));
					String tel = Utils.propertyToShow(so.getProperty(4));
					String mobile = Utils.propertyToShow(so.getProperty(5));
					String intro = Utils.propertyToShow(so.getProperty(6));
					String business = Utils.propertyToShow(so.getProperty(7));
					String creditScore = Utils
							.propertyToShow(so.getProperty(8));
					String finishOrderCount = Utils.propertyToShow(so
							.getProperty(9));

					mPlace.setText(place);
					mName.setText(name);
					mContact.setText(contact);
					mTel.setText(tel);
					mTelDial.setTag(tel);
					mMobile.setText(mobile);
					mMobileDial.setTag(mobile);
					mIntro.setText(intro);
					mBusiness.setText(business);
					mCreditScore.setText(creditScore);
					mFinishOrderCount.setText(finishOrderCount);
					mXinyongScore.setText(Utils.propertyToShow(so
							.getProperty(12)));
					userID = Utils.propertyToShow(so
							.getProperty(13));
				} else {
					Toast.makeText(LogisticCompanyInfo.this, "获取信息失败，请稍候再试",
							Toast.LENGTH_LONG).show();
				}
				mDialog.dismiss();
			};

			@Override
			protected void onPreExecute() {
				mDialog = ProgressDialog.show(LogisticCompanyInfo.this, "",
						"加载中");
			};

			@Override
			protected Object doInBackground(Void... params) {
				try {
					return mWebServiceClient.commonFindLogisticCompanyDetail(
							mCertificate, mLogisticCompanyId);
				} catch (IOException e) {
					return e;
				} catch (XmlPullParserException e) {
					return e;
				}
			}
		}.execute();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.xinyongdetail: {
			Intent i = new Intent(this, Xinyongxingji.class);
			i.putExtra("userID", userID);
			startActivity(i);
		}
			break;
		case R.id.allSpecialLine: {
			Intent i = new Intent(this, FindSpecialLineResult.class);
			i.putExtra("isAll", true);
			i.putExtra("userID", userID);
			startActivity(i);
		}
			break;
		case R.id.tel_dial:
		case R.id.mobile_dial: {
			Intent i = new Intent(Intent.ACTION_DIAL);
			i.setData(Uri.parse("tel:" + v.getTag().toString()));
			startActivity(i);
		}
			break;
		case R.id.collect:
			break;
		}
	}
}
